<template>
  <n-modal v-model:show="modelValue.toMove" :mask-closable="false" preset="dialog" title="确认" content="你确认"
    positive-text="确认" negative-text="算了" @positive-click="onPositiveClick" @negative-click="modelValue.toMove = false">
    原来的领域
    <n-tree-select v-model:value="modelValue.oldFieldId" :options="fieldSelect" filterable
      :disabled="modelValue.toMove" />
    原来的目标
    <n-select v-model:value="modelValue.oldGoalId" :options="goalSelect" filterable :disabled="modelValue.toMove" />
    请选择领域
    <n-tree-select v-model:value="modelValue.fieldId" :options="fieldSelect" filterable />
    请选择目标
    <n-select v-model:value="modelValue.goalId" :options="goalSelect" filterable />
  </n-modal>
</template>
<script lang="ts" setup>
import { NModal, useMessage } from "naive-ui";
import { computed, onMounted, ref, watch } from "vue";
import { post, Response } from "@/api/http";
import { getFieldSelect, getGoalSelect, getStageSelect, moveStage } from "@/api/url";
import useUserStore from "@/store/modules/user";

// 全局数据
const message = useMessage();
const userStore = useUserStore()

// 父组件数据
const props = defineProps<{
  modelValue: {
    id: number
    toMove: boolean
    stageId: number
    fieldId: number
    goalId: number
    oldFieldId: number
    oldGoalId: number
    state: number
    [key: string]: any
  }
}>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});

const goalSelect = ref<Array<any>>([]);
const fieldSelect = ref<Array<any>>([]);
const getGoalAndFieldSelect = async () => {
  try {
    const data: Response = await post({
      url: getGoalSelect,
      data: { haveFinish: false },
      token: userStore.getToken()
    });
    // console.log(data)
    goalSelect.value = data.data.data
    // console.log(goalSelect)
  } catch (e) {
    message.error((e as Error).message)
  } finally {
  }
  try {
    const data: Response = await post({
      url: getFieldSelect,
      token: userStore.getToken()
    });
    // console.log(data)
    fieldSelect.value = data.data.data
    // console.log(fieldSelect)
  } catch (e) {
    message.error((e as Error).message)
  } finally {
  }
}
onMounted(getGoalAndFieldSelect)
const onPositiveClick = async () => {
  if (modelValue.value.fieldId === modelValue.value.oldFieldId && modelValue.value.goalId === modelValue.value.oldGoalId)
    message.error('未发生变化')
  else {
    try {
      const data: Response = await post({
        url: moveStage,
        data: { id: modelValue.value.id, fieldId: modelValue.value.fieldId, goalId: modelValue.value.goalId, state: modelValue.value.state },
        token: userStore.getToken()
      });
    } catch (e) {
      message.error((e as Error).message)
    } finally {
    }
  }

};

// 任务栏选项

</script>
